package com.ceshiren.aitestmini.util;

import com.alibaba.fastjson.JSONObject;
import com.ceshiren.aitestmini.common.Constants;
import com.ceshiren.aitestmini.dto.task.OperateJenkinsJobDto;
import com.ceshiren.aitestmini.dto.task.RequestInfoDto;
import com.ceshiren.aitestmini.entity.HogwartsTestTask;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.Job;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * @Author tlibn
 * @Date 2020/8/11 15:21
 **/
public class JenkinsUtil {

    public static R build2(OperateJenkinsJobDto operateJenkinsJobDto) throws IOException, URISyntaxException {

        String jenkinsUrl = operateJenkinsJobDto.getJenkinsUrl();
        String jenkinsUserName = operateJenkinsJobDto.getJenkinsUserName();
        String jenkinsPassword = operateJenkinsJobDto.getJenkinsPassword();

        //构建参数
        Map<String, String> params = operateJenkinsJobDto.getParams();


        String jobConfigXml =getJobXml("jenkins/hogwarts_jenkins_test_start.xml");

        JenkinsHttpClient jenkinsHttpClient = new JenkinsHttpClient(new URI(jenkinsUrl),jenkinsUserName, jenkinsPassword);

        JenkinsServer jenkinsServer = new JenkinsServer(jenkinsHttpClient);

        try{
            jenkinsServer.updateJob(Constants.JOB_NAME,jobConfigXml,true);
        }catch (Exception e){
            jenkinsServer.createJob(Constants.JOB_NAME,jobConfigXml,true);
        }

        Map<String, Job> jonMap = jenkinsServer.getJobs();
        Job job = jonMap.get(Constants.JOB_NAME);
        job.build(params, true);

        return R.ok().message("成功");

    }

    public static String getJobXml(String path) throws IOException {

        ClassPathResource classPathResource = new ClassPathResource(path);
        InputStream inputStream = classPathResource.getInputStream();

        String jobConfigXml = FileUtil.getText(inputStream);

        return jobConfigXml;

    }

    /**
     *  拼装更新任务状态的url
     * @param requestInfoDto
     * @param hogwartsTestTask
     * @return
     */
    public static StringBuilder makeUpdateTaskStatusUrl(RequestInfoDto requestInfoDto, HogwartsTestTask hogwartsTestTask) {

        StringBuilder updateTaskStatusUrl = new StringBuilder();

        updateTaskStatusUrl.append("curl -X PUT ");
        updateTaskStatusUrl.append("\"" + requestInfoDto.getBaseUrl() +"project/task/status\" ");
        updateTaskStatusUrl.append("-H \"Content-Type:application/json\" ");
        updateTaskStatusUrl.append("-d ");
        JSONObject json = new JSONObject();
        json.put("taskId",hogwartsTestTask.getId());
        json.put("buildUrl","${BUILD_URL}");
        json.put("status", Constants.STATUS_THREE);
        updateTaskStatusUrl.append("'"+json.toJSONString()+"'");

        return updateTaskStatusUrl;

    }




    public static String getAllureReportUrl(String buildUrl){
        return buildUrl + "allure";
    }



}
